package com.moss.cloud.common.core.utils;

import lombok.extern.slf4j.Slf4j;

import java.util.Arrays;
import java.util.Optional;
import java.util.function.Predicate;

/**
 * @Author: songLei
 * @Description:
 * @Date: 2020/12/23 20:21
 * @Version: 1.0
 */
@Slf4j
public class EnumsUtils {

    /**
     * 根据条件获取枚举对象
     * @param enumClass 枚举类
     * @param predicate 筛选条件
     * @param <T>
     * @return
     */
    public static <T extends Enum<T>> Optional<T> getEnumObject(Class<T> enumClass, Predicate<T> predicate) {
        if (!enumClass.isEnum()) {
            log.info("Class 不是枚举类");
            return Optional.empty();
        }
        T[] enumConstants = enumClass.getEnumConstants();
        return Arrays.stream(enumConstants).filter(predicate).findAny();
    }
}
